// subPages/glucometer/history/history.js
const app = getApp()
var jsonData = require('../../../data/booldSugarJson');
var util =  require('../../../utils/util.js');
var template = require('../../../template/template.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    haveData: 1,
    tabBarShow: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.item != null && options.item != 'undefined'){
      var item =  JSON.parse(options.item)
      that.setData({
        tabBarShow: false
      })
      that.getHistoryList(item.userId)
      wx.setNavigationBarTitle({
        title: item.nickName + '的血糖历史' //页面标题为路由参数
      })
    }else{
      template.SugarTabbar("tabBar", 1, this) //0表示第一个tabbar
      this.getHistoryList()
    }
  },

  getHistoryList:function(userId){
    var that = this
    var id = ''
    if(userId != '' && userId != 'undefined' && userId != null){
      id = userId
    }else{
      id = app.globalData.userId
    }
    wx.request({
      url: app.globalData.apiUrl +'/sugar',
      data: {
        userId: id
      },
      method: 'post',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          if(res.data.data){
            that.setData({
              historyList: res.data.data
            })
            that.historyItem(that.data.historyList)
          }else{
            that.setData({
              haveData: 2,
              tipTxt: {
                tipTxt: '暂无测量数据',
                tipImg: '/resources/index-image/noData.png'
              }
            })
          }
        }else{
          that.setData({
            haveData: 2,
            tipTxt: {
              tipTxt: '该用户没有绑定血糖仪设备',
              tipImg: '/resources/index-image/noData.png'
            }
          })
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //处理数据
  historyItem:function(historyList){
    var that = this
    var bloodSugarList = jsonData.bloodSugarHistoryList
    for(var i in historyList)
    {
      for(var j in historyList[i].list)
      {
        var item = historyList[i].list[j]
        if(item.type == 1)
        {
          bloodSugarList[0].time = util.setTime2(item.createtime);
          bloodSugarList[0].blood = item.blood;
          bloodSugarList[0].status = item.status;
        }
        if(item.type == 2)
        {
          bloodSugarList[1].time = util.setTime2(item.createtime);
          bloodSugarList[1].blood = item.blood;
          bloodSugarList[1].status = item.status;
        }
        if(item.type == 3)
        {
          bloodSugarList[2].time = util.setTime2(item.createtime);
          bloodSugarList[2].blood = item.blood;
          bloodSugarList[2].status = item.status;
        }
        if(item.type == 4)
        {
          bloodSugarList[3].time = util.setTime2(item.createtime);
          bloodSugarList[3].blood = item.blood;
          bloodSugarList[3].status = item.status;
        }
        if(item.type == 5)
        {
          bloodSugarList[4].time = util.setTime2(item.createtime);
          bloodSugarList[4].blood = item.blood;
          bloodSugarList[4].status = item.status;
        }
        if(item.type == 6)
        {
          bloodSugarList[5].time = util.setTime2(item.createtime);
          bloodSugarList[5].blood = item.blood;
          bloodSugarList[5].status = item.status;
        }
      }
      var bloodSugar = "historyList[" + i + "].bloodSugarList";
      var time = "historyList[" + i + "].time"
      var createtime = "historyList[" + i + "].createtime"; //原本时间格式
      that.setData({
        [bloodSugar] : bloodSugarList,
        [createtime]: historyList[i].time,
        [time]: util.setTime(historyList[i].time)
      })
    }
  },

  //血糖趋势
  bloodSugarTrend:function(){
    wx.navigateTo({
      url: '../bloodSugarTrend/bloodSugarTrend'
    }) 
  },

  //查看详情
  detailsBtn:function(e){
    wx.navigateTo({
      url: '../details/details?time=' + e.currentTarget.dataset.time
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})